package com.cb.orderservice.controller;


import com.cb.orderservice.service.TPayLogService;
import com.cb.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author Cebb
 * @since 2020-06-29
 */
@Api("支付日志管理")

@RestController
@RequestMapping("/orderservice/paylog")
public class TPayLogController {

    private final TPayLogService payLogService;

    @Autowired
    public TPayLogController(TPayLogService payLogService) {
        this.payLogService = payLogService;
    }

    @ApiOperation("生成微信支付二维码")
    @GetMapping("createNative/{orderNo}")
    public R createNative(
            @ApiParam(name="orderNo",value = "订单号",required = true)
            @PathVariable("orderNo") String orderNo) {
        // 返回二维码地址, 和其他信息
        Map<String,Object> map = payLogService.createNative(orderNo);

        return R.ok().data(map);
    }

    @ApiOperation("根据订单号查询支付状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable("orderNo") String orderNo){
        Map<String,String> map = payLogService.queryPayStatus(orderNo);

        if (map == null) {
            return R.error().message("支付信息异常...");
        }
        // 如果为success,表示支付成功
        if (map.get("trade_state").equals("SUCCESS")) {
            // 添加记录到支付表, 更新订单表的订单状态
            payLogService.updateOrdersStatus(map);
            return R.ok().message("支付成功!");
        }

        return R.ok().code(25000).message("支付中...");
    }

}

